package com.kpiJavaTraining.bogdan.module2;

import java.io.Serializable;

/**
 * 
 * @author Bogdan Olexandr
 */
public class TestStack implements Serializable{

	private static final long serialVersionUID = -7295414730618511379L;
	private User user;
	private ListOfNode<Question> questions;
	private int indexOfCurrentQuestion;
	private int score;
	
	public TestStack(ListOfNode<Question> list, User user) throws CloneNotSupportedException{
		questions = list.clone();
		setUser(user);
	}
		
	public int getCountOfQuestions(){
		return questions.getCountOfNode();
	}
		
	public void answer(String results){
		questions.getNode(this.getIndexOfCurrentQuestion()).answer(results);
		setScore();
		addIndexOfCurrentQuestion();
	}

	public int getIndexOfCurrentQuestion(){
		return indexOfCurrentQuestion;
	}

	private void addIndexOfCurrentQuestion(){
		indexOfCurrentQuestion++;
	}
	
	private void reduceIndexOfCurrentQuestion(){
		indexOfCurrentQuestion--;
	}

	public int getScore(){
		return score;
	}

	private void setScore() {
		score += questions.getNode(indexOfCurrentQuestion).getScore();	
	}
	
	public void prevQuestion(){
		if(getIndexOfCurrentQuestion() > 0){
			reduceIndexOfCurrentQuestion();
			score -= questions.getNode(indexOfCurrentQuestion).getScore();
			questions.getNode(indexOfCurrentQuestion).clear();
		}
	}
	
	public String toString(){
		return questions.getNode(indexOfCurrentQuestion).toString();
	}

	public User getUser() {
		return user;
	}

	private void setUser(User user) {
		this.user = user;
	}
}